
package com.vanda.db;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import com.vanda.data.BeanListBase;
import com.vanda.db.cfg.ConfigBean;
import com.vanda.db.cfg.MappingInfo;
import com.vanda.db.exception.CreateException;
import com.vanda.db.exception.DbException;
import com.vanda.db.exception.MappingException;
import com.vanda.util.Globals;
import com.vanda.util.LogFunc;

/*******
 * 类说明:
 * 数据库操作类，封装Session类
 * <P>@author 王瑛
 * <P>@version 1.0 2005-2-26
 */
public abstract class Accessor {
	/** Hibernate Session */
	protected Session session = null;
	/** 事务管理 */
	protected Transaction tx = null;
	/** 转换Accessor */
	protected Accessor adaptAccessor = null;
	
	protected static int MAXLENGTH = Globals.getIntValue("db.query.maxlength")+1;
	
	/**
	 * 构造方法
	 * @param session 
	 */
	protected Accessor(Session session) {
		this.session = session;
	}
	
	/** 
	 * 返回Session对象
	 * @return
	 */
	public Session getSession() {
		return session;
	}
	
	/**
	 * End the Session by disconnecting from the JDBC connection and cleaning up.
	 * @throws DbException
	 */
	public void close() throws DbException {
		try {
			HibernateUtil.closeSession(session);
			session = null;
		} catch (HibernateException he) {
			he.printStackTrace();
			throw new DbException(he);
		}
	}
	
	/**
	 * 开始事务处理
	 * @throws DbException
	 */
	public void begin() throws DbException {
		try {
			tx = session.beginTransaction();
		} catch (HibernateException he) {
			he.printStackTrace();
			throw new DbException(he);
		}
	}
	
	/**
	 * 事务回滚
	 * @throws DbException
	 */
	public void rollback() throws DbException {
		try {
			if (tx != null) {
				tx.rollback();
			}
		} catch (HibernateException he) {
			he.printStackTrace();
			throw new DbException(he);
		}
	}
	
	/**
	 * 事务提交
	 * @throws DbException
	 */
	public void commit() throws DbException {
		try {
			if (tx != null) {
				tx.commit();
			}
		} catch (HibernateException he) {
			he.printStackTrace();
			throw new DbException(he);
		}
	}
	
	/**
	 * 查找xql对应的xml配置信息
	 * @param name xql名称，例如："order:findOrder"
	 * @return 
	 * @throws MappingException
	 */
	protected static MappingInfo findMapping(String name)
		throws MappingException {
		MappingInfo info = ConfigBean.findMappingInfo(name);
		if (info == null) {
			throw new MappingException(
				"XML文件没有找到名字为[" + name + "]的配置信息，请检查文件！");
		}
		return info;
	}
	
	/**
	 * 查询方法
	 * @param name xql名称
	 * @param dataBean 查询参数
	 * @return 查询结果
	 * @throws DbException 操作数据库失败
	 */
	public abstract BeanListBase queryName(String name, Object dataBean)
		throws DbException;
		
	/**
	 * 查询方法
	 * @param name xql名称
	 * @param dataBean 查询参数
	 * @return 返回查询结果第一条记录，如果没有查询结果，则返回null
	 * @throws DbException 操作数据库失败
	 */
	public abstract Object findName(String name,Object dataBean)
		throws DbException;
	
	public SQLAccessor getSQLAccessor() throws CreateException{
		if(this instanceof SQLAccessor){
			return (SQLAccessor)this;
		}else{
			return ((HQLAccessor)this).adapter();
		}
		
	}
	
	public HQLAccessor getHQLAccessor() throws CreateException{
		if(this instanceof HQLAccessor){
			return (HQLAccessor)this;
		}else{
			return ((SQLAccessor)this).adapter();
		}
		
	}
	
	/**
	 * 检查查询结果是否超出系统最大值
	 * @param errorCode
	 * @param xql
	 */
	protected void checkQueryLimit(int size,String xql) throws DbException{
		if(size <= MAXLENGTH - 1){
			return;
		}
		
		DbException de = new DbException(DbException.QUERY_OVER_MAXRESULT);
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(de.getMessage()).append(":").append(de.getErrorMsg());
		buffer.append("\nQuery XQL : ").append(xql);
		
		LogFunc.error(buffer.toString());
		throw new DbException(DbException.QUERY_OVER_MAXRESULT);
	}
}
